/**
 * 初始化详情对话框
 */
var InsuranceInfoDlg = {
    insuranceInfoData : {},
    validateFields: {
    	insuranceCode: {
            validators: {
                notEmpty: {
                    message: '保险公司代码不能为空'
                },
                stringLength:{
                	max: 4,
                    less: '保险公司代码最多只能输入4位',
                }
            }
        },
        insuranceAbbr: {
            validators: {
                notEmpty: {
                    message: '保险公司简称不能为空'
                },
                stringLength:{
                	max: 20,
                    less: '保险公司简称最多只能输入20位',
                }
            }
        },
        insuranceName: {
            validators: {
                notEmpty: {
                    message: '保险公司名称不能为空'
                },
                stringLength:{
                	max: 50,
                    less: '保险公司名称最多只能输入50位',
                }
            }
        },
        creditCode: {
            validators: {
                notEmpty: {
                    message: '社会信用代码不能为空'
                },
                stringLength:{
                	min:22,
                	max:22,
                	message: '社会信用代码长度必须是18位'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
InsuranceInfoDlg.clearData = function() {
    this.insuranceInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
InsuranceInfoDlg.set = function(key, val) {
    this.insuranceInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
InsuranceInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
InsuranceInfoDlg.close = function() {
    parent.layer.close(window.parent.Insurance.layerIndex);
}

/**
 * 收集数据
 */
InsuranceInfoDlg.collectData = function() {
    this
    .set('id')
    .set('createdBy')
    .set('createdDate')
    .set('lastModifiedBy')
    .set('lastModifiedDate')
    .set('insuranceCode')
    .set('insuranceName')
    .set('insuranceAbbr')
    .set('creditCode')
    .set('accessoryNum')
    .set('isDel');
}

/**
 * 验证数据是否为空
 */
InsuranceInfoDlg.validate = function () {
    $('#insuranceInfoForm').data("bootstrapValidator").resetForm();
    $('#insuranceInfoForm').bootstrapValidator('validate');
    return $("#insuranceInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
InsuranceInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    //校验
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/insurance/add", function(data){
        Feng.success("添加成功!");
        window.parent.Insurance.table.refresh();
        InsuranceInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.insuranceInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
InsuranceInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //校验
    if (!this.validate()) {
        return;
    }
    
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/insurance/update", function(data){
        Feng.success("修改成功!");
        window.parent.Insurance.table.refresh();
        InsuranceInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.insuranceInfoData);
    ajax.start();
}

/**
 * 将所输入的社会信用代码按照对应的文本格式处理X-X-XXXXXX-XXXXXXXXX-X后返回
 */
InsuranceInfoDlg.resetCreditCode = function() { 
	var newCreditCode = '';
	var creditCode = $('#creditCode').val().replace(/-/g,"");
	var creditCodeLength = creditCode.length;
	if(creditCodeLength>17){
		newCreditCode = creditCode.substr(0,1) + '-' + creditCode.substr(1,1) + '-' + creditCode.substr(2,6) + '-' + creditCode.substr(8,9) + '-' + creditCode.substr(17,1);
	}else if(creditCodeLength>8){
		newCreditCode = creditCode.substr(0,1) + '-' + creditCode.substr(1,1) + '-' + creditCode.substr(2,6) + '-' + creditCode.substr(8,9);
	}else if(creditCodeLength>2){
		newCreditCode = creditCode.substr(0,1) + '-' + creditCode.substr(1,1) + '-' + creditCode.substr(2,6);
	}else if(creditCodeLength>1){
		newCreditCode = creditCode.substr(0,1) + '-' + creditCode.substr(1,1);
	}else{
		newCreditCode = creditCode.substr(0,1);
	}
	return newCreditCode;
}

$(function() {
	Feng.initValidator("insuranceInfoForm", InsuranceInfoDlg.validateFields);
	
	//社会信用代码实时按照对应的文本格式处理X-X-XXXXXX-XXXXXXXXX-X
	$('#creditCode').bind('input propertychange', function() {
		var creditCode = InsuranceInfoDlg.resetCreditCode();
		$('#creditCode').val(creditCode);
	});
});
